<template>
  <el-card shadow="never" class="card-box">
    <el-form :model="queryForm" label-width="80px">
      <el-row :gutter="20">
      </el-row>
      <el-row :gutter="20">
        <el-col :span="9">
          <el-form-item label="搜索">
            <el-input v-model="queryForm.keyword" @keyup.enter="onSearch" clearable placeholder="请输入关键词"/>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="ID">
            <el-input v-model="queryForm.id" @keyup.enter="onSearch" clearable placeholder="请输入ID"/>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="语言类型">
            <el-select
                v-model="queryForm.type"
                value-key="id"
                clearable
                @clear="onType"
                placeholder="请选择语言类型"
            >
              <el-option
                  v-for="item in typeList"
                  :key="item.name"
                  :label="item.message"
                  :value="item.name"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="展示状态">
            <el-select
                v-model="queryForm.exhibitStatus"
                value-key="id"
                clearable
                @clear="onExhibitStatusType"
                placeholder="请选择展示状态"
            >
              <el-option
                  v-for="item in exhibitStatusList"
                  :key="item.name"
                  :label="item.message"
                  :value="item.name"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="24" :md="12" :lg="8" :xl="6">
          <el-form-item label-width="0">
            <el-button type="primary" @click="onSearch">
              <el-icon>
                <ele-search/>
              </el-icon>
              <span class="search-btn__left">查询</span>
            </el-button>
            <el-button @click="onReset">
              <el-icon>
                <ele-refresh/>
              </el-icon>
              <span class="search-btn__left">重置</span>
            </el-button>
            <el-button v-auth="'article:add'" type="primary" @click="openDialog()">
              <el-icon class="mr5">
                <ele-circle-plus/>
              </el-icon>
              新 增
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="tableLoading.status" :data="tableData.data" border row-key="id">
      <el-table-column prop="_tableIndex" label="序号" align="center" width="50px"/>
      <el-table-column prop="id" label="ID" align="center" show-overflow-tooltip/>
      <el-table-column prop="title" label="文章标题" align="center" show-overflow-tooltip/>
      <el-table-column label="文章内容" align="center">
        <template #default="scope">
          <el-tooltip
              class="box-item"
              effect="dark"
              :content="scope.row.content"
              placement="top"
              :raw-content="true"
          >
            <template #content>
              <div v-html="scope.row.content"
                   :style="{ 'overflow': 'hidden', 'text-overflow': 'ellipsis', 'white-space': 'nowrap' ,'width': '400px'}"></div>
            </template>
            <div v-html="scope.row.content" style="height: 50px"></div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column property="imgUrl" align="center" label="图片">
        <template #default="scope">
          <el-image default="scope"
                    style="width: 50px; height: 50px"
                    :src="scope.row.images"
                    :zoom-rate="1.2"
                    :max-scale="7"
                    :min-scale="0.2"
                    :preview-src-list="[scope.row.images]"
                    :initial-index="1"
                    :hide-on-click-modal="true"
                    :preview-teleported="true"
                    fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column prop="type" label="语言类型" align="center">
        <template #default="scope">
          <div>
            <el-tag v-text="scope.row.type.message"></el-tag>
          </div>
        </template>
      </el-table-column>
      <!--      <el-table-column prop="categoryId" label="文章分类" align="center" show-overflow-tooltip/>-->
      <el-table-column prop="browseCount" label="浏览量" align="center" show-overflow-tooltip/>
      <el-table-column prop="exhibitStatus.message" label="展示状态" align="center" show-overflow-tooltip>
        <template #default="scope">
          <div>
            <el-tag v-text="scope.row.exhibitStatus.message"></el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" min-width="100" sortable="custom"/>
      <el-table-column label="操作" fixed="right" align="center" min-width="116">
        <template #default="scope">
          <el-button v-if="scope.row.type.code == 'RAW'" link type="primary"
                     @click="updateExhibitStatusTable(scope.row)">
            <el-icon>
              <ele-open/>
            </el-icon>
            {{ scope.row.exhibitStatus.code === 'EXHIBIT' ? '不展示' : '展示' }}
          </el-button>
          <el-button v-if="scope.row.type.code == 'RAW'" v-auth="'article:update'" link type="primary"
                     @click="openTranslationDialog(scope.row)">
            <el-icon>
              <ele-chat-square />
            </el-icon>
            翻译
          </el-button>
          <el-button v-auth="'article:update'" link type="primary" @click="openDialog(scope.row)">
            <el-icon>
              <ele-edit/>
            </el-icon>
            修改
          </el-button>
          <el-button v-auth="'article:delete'" link type="primary" @click="delTable(scope.row)">
            <el-icon>
              <ele-delete/>
            </el-icon>
            删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <CustomPagination v-model:currentPage="pagination.pageIndex" v-model:pageSize="pagination.pageSize"
                      :total="pagination.total" @changePage="changePage"/>

    <TableForm ref="tableDialogRef" @refresh="getTableList"/>
    <translationSelect ref="translationDialogRef" @refresh="getTableList"/>

  </el-card>
</template>
<script lang="ts" setup>
import {deleteArticle, getArticlePage, updateExhibit} from "@/api/app/article";
import {ElMessage, ElMessageBox} from 'element-plus'
import TableForm from './form.vue'
import translationSelect from './translationSelect.vue'
import {calcTableIndex} from "@/utils/util";

/** 查询参数 **/
let queryForm: any = ref({
  id: null,
  keyword: null,
  type: null,
  exhibitStatus: null
});

const onType = () => {
  queryForm.value.type = null;
}

const onExhibitStatusType = () => {
  queryForm.value.exhibitStatus = null;
}

const typeList = ref([
  {'name': 'RAW', 'message': '原文'},
  {'name': 'ZH', 'message': '中文'},
  {'name': 'EN', 'message': '英文'}
])

const exhibitStatusList = ref([
  {'name': 'EXHIBIT', 'message': '展示'},
  {'name': 'NOT_EXHIBIT', 'message': '不展示'}
])

const tableLoading = ref({
  status: false
})

// 查询
const onSearch = () => {
  pagination.pageIndex = 1;
  getTableList();
}
// 重置
const onReset = () => {
  queryForm.value = {}
  pagination.pageIndex = 1;
  getTableList();
}

/** 分页*/
// 分页数据
const pagination = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0
})
// 翻页
const changePage = (page: number) => {
  pagination.pageIndex = page;
  getTableList();
}

/** 表格*/
// 表格数据
const tableData = reactive({
  data: [],
})
// 获取表格列表
const getTableList = () => {
  tableLoading.value.status = true;
  tableData.data = []
  getArticlePage({...pagination, ...queryForm.value}).then(res => {
    tableData.data = calcTableIndex(res, pagination);
    pagination.total = res.total;
    tableLoading.value.status = false;
  })
}

// 修改状态
const updateExhibitStatusTable = (data: any) => {
  let exhibitStatus = 'EXHIBIT'
  let title = "是否确认展示本条数据？"
  if(data.exhibitStatus.code === 'EXHIBIT'){
    title = "是否确认不展示本条数据？"
    exhibitStatus = 'NOT_EXHIBIT'
  }
  let value = {
    "id": data.id,
    "exhibitStatus": exhibitStatus
  }
  ElMessageBox.confirm(
      title,
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(() => {
        updateExhibit(value).then(() => {
          ElMessage.success('设置成功');
          getTableList();
        })
      }).catch(() => {
  })
}


// 删除
const delTable = (data: any) => {
  let value = {
    "id": data.id,
    "type": data.type.code
  }
  let title = "是否确认删除本条数据？"
  if (data.type.code === "RAW") {
    title = "是否确认删除原文（删除原文译文也会被删除）？"
  }
  ElMessageBox.confirm(
      title,
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(() => {
        deleteArticle(value).then(() => {
          ElMessage.success('删除成功');
          getTableList();
        })
      }).catch(() => {
  })
}

/** 添加，编辑*/
const tableDialogRef = ref()
// 打开弹框
const openDialog = async (data: any) => {
  await tableDialogRef.value.openDialog(data);
}

const translationDialogRef = ref()
// 打开弹框
const openTranslationDialog = async (data: any) => {
  await translationDialogRef.value.openTranslationDialog(data);
}

getTableList();
</script>
<style scoped>

</style>
